import wave
import pyaudio
from aip import AipSpeech

def createAudioFile():
        # 定义数据流块
        CHUNK = 1024
        FORMAT = pyaudio.paInt16
        # 想要百度识别，下面这两参数必须这样设置，使得比特率为256kbps
        CHANNELS = 1
        RATE = 16000
        # 录音时间
        RECORD_SECONDS = 5
        # 要写入的文件名
        WAVE_OUTPUT_FILENAME = "output.wav"
        # 创建PyAudio对象
        p = pyaudio.PyAudio()

        # 打开数据流
        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)

        print("* recording")

        # 开始录音
        frames = []
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)

        print("* done recording")
        # 停止数据流
        stream.stop_stream()
        stream.close()

        # 关闭PyAudio
        p.terminate()

        # 写入录音文件
        wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(frames))
        wf.close()